package com.yanggu.spark.sql

import java.util.Properties

import org.apache.spark.sql.{SaveMode, SparkSession}

case class User05(name: String, age: Long, email: String)
//spark-sql将数据保存在mysql中
object SparkSQL05_Write_Mysql {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("SparkSQL05_Write_Mysql")
      .getOrCreate()
    import spark.implicits._

    val ds = spark.sparkContext.makeRDD(List[User05](User05("赵六", 30, "qq.com"))).toDS()

    //方式1：通用的方式  format指定写出类型
    ds.write
      .format("jdbc")
      .option("url", "jdbc:mysql://localhost:3306/yanggu")
      .option("user", "root")
      .option("password", "root")
      .option("dbtable", "user")
      .mode(SaveMode.Append)
      .save

    //方式2: JDBC的方式
    val props = new Properties()
    props.setProperty("user", "root")
    props.setProperty("password", "root")
    ds.write
      .mode(SaveMode.Append)
      .jdbc("jdbc:mysql://localhost:3306/yanggu", "user", props)

  }

}
